package com.example.dishfo.myapplication.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.Window;

import com.example.dishfo.myapplication.activity.showimgae.ShowImageActivity;

import java.lang.reflect.Field;
import java.util.ArrayList;

public class ActivityUtil {
    public static Editable getEditText(TextInputLayout textInputLayoutEmail) {
        return textInputLayoutEmail.getEditText().getText();
    }

    public static void fullScreen(Activity mainActivity) {
        Window window=mainActivity.getWindow();
        View view=window.getDecorView();
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = mainActivity.getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            mainActivity.getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    public static void clearTextInputError(TextInputLayout textInputLayout){
        textInputLayout.setError("");
        textInputLayout.setErrorEnabled(false);
    }

    public static void setTransportStatusBar(Window window){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            try {
                Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView");
                Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor");
                field.setAccessible(true);
                field.setInt(window.getDecorView(), Color.TRANSPARENT);  //改为透明
            } catch (Exception e) {

            }
        }
    }

    @SuppressLint("RestrictedApi")
    public static void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        }
    }


    public static void showImages(Activity activity, ArrayList<String> urls,int index){
        Intent intent=new Intent(activity, ShowImageActivity.class);
        intent.putStringArrayListExtra(ShowImageActivity.URLS_LABEL,urls);
        intent.putExtra(ShowImageActivity.CUR_INDEX,index);
        activity.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(activity).toBundle());
    }
}
